package com.tianqu.tms.www.car.controller;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.util.ImageUtils;
import com.tianqu.tms.core.util.Registry;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.core.webservice.weichat.api.MobileMessage;
import com.tianqu.tms.core.webservice.weichat.api.PullingExecutor;
import com.tianqu.tms.core.webservice.weichat.api.rpc.TransferDataSyn1305Bo;
import com.tianqu.tms.dto.TmsFinanciaDto;
import com.tianqu.tms.dto.TmsMarketCarFinancialDto;
import com.tianqu.tms.entity.Car;
import com.tianqu.tms.entity.TmsFinancial;

public class ChangeFinancialStatusController  extends TmsController {

	private Logger logger = Logger.getLogger(ChangeFinancialStatusController.class);

	@SuppressWarnings("unchecked")
	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();
		// 添加事务提交异常捕获
		try {
			String id = uc.getParameter("id");
			String remark = uc.getParameter("remark");
			controller.begin();
			TmsMarketCarFinancialDto dto = new TmsMarketCarFinancialDto();
			dto.setId(Integer.parseInt(id));
			dto.setStatus(2);
			dto.setRemark(remark);
			dto.setUpdateUserId(1);
			//
			TmsFinancial financial = TmsFinancial.getInstance();
			financial.updateTmsMarketCarFinancial(dto);
			
			
			dto = financial.getTmsMarketCarFinancial(id);
			if(dto!=null) {
				Car car = Car.getInstance().findCarBySerial(dto.getSerial());
				//1305
				//微信同步
				MobileMessage message = new MobileMessage();
				message.putValue("serial", dto.getSerial());
				message.setMessage("金融车辆调用接口失败,商户ID为#serial#");
				
				String img1 = ImageUtils.getImageDirectoryPath(Registry.getConfig(TmsConstants.UPLOAD_FINANCIA_PATCH_KEY)+dto.getSerial()+"/")+dto.getSerial()+"_1.jpg";
				String img2 = ImageUtils.getImageDirectoryPath(Registry.getConfig(TmsConstants.UPLOAD_FINANCIA_PATCH_KEY)+dto.getSerial()+"/")+dto.getSerial()+"_2.jpg";
				String img3 = ImageUtils.getImageDirectoryPath(Registry.getConfig(TmsConstants.UPLOAD_FINANCIA_PATCH_KEY)+dto.getSerial()+"/")+dto.getSerial()+"_3.jpg";
				String img4 = ImageUtils.getImageDirectoryPath(Registry.getConfig(TmsConstants.UPLOAD_FINANCIA_PATCH_KEY)+dto.getSerial()+"/")+dto.getSerial()+"_5.jpg";
				String reportPhoto = !StringUtil.isNullOrBlank(dto.getReportPhoto())?ImageUtils.transPicToStr(img1):"";
				String materialImages1 = !StringUtil.isNullOrBlank(dto.getMaterialImages1())?ImageUtils.transPicToStr(img2):"";
				String materialImages2 = !StringUtil.isNullOrBlank(dto.getMaterialImages2())?ImageUtils.transPicToStr(img3):"";
				String materialImages3 = !StringUtil.isNullOrBlank(dto.getMaterialImages3())?ImageUtils.transPicToStr(img4):"";
				
				TransferDataSyn1305Bo api = new TransferDataSyn1305Bo();
				api.setSerial(dto.getSerial());
				api.setFinancialSerial(dto.getFinanciaIdSerial());
				api.setFinanciaId(dto.getFinanciaId());
				api.setMemberId(dto.getMemberId());
				api.setStatus(dto.getStatus());
				api.setRegisterStatus(car.getRegisterStatus());
				api.setBarCodeNum(car.getBarCodeNum());
				api.setLicensePlate(car.getLicensePlate());
				api.setVin(car.getVin());
				api.setAttestStatus(car.getAttestStatus());
				api.setReportPhoto(reportPhoto);
				api.setValuations(dto.getValuations());
				api.setLoanLimit(dto.getLoanLimit());
				api.setStartDate(dto.getStartDate());
				api.setEndDate(dto.getEndDate());
				api.setMaterialImages1(materialImages1);
				api.setMaterialImages2(materialImages2);
				api.setMaterialImages3(materialImages3);
				api.setRemark(dto.getRemark());
				api.setInsertTime(dto.getInsertTime());
				api.setInsertUserId(dto.getInsertUserId());
				api.setUpdateTime(dto.getUpdateTime());
				api.setUpdateUserId(dto.getUpdateUserId());
				api.setMarketNo(TmsConstants.MARKET_NO);
				PullingExecutor.getInstance().submit(api);
			}
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}
	}
}
